library(shiny)
library(tidyverse)

# Load precomputed data

sct_data <- readRDS(file = "data/rds/proj.rds")
sct_data_b <- readRDS(file = "data/rds/categories.rds")
choropleth_data <- readRDS(file = "data/rds/choropleth_data.rds")
parallelcoordinates_data <- readRDS(file = "data/rds/parallelcoordinates_data.rds")
bar_data  <- readRDS(file = "data/rds/bar_data.rds")
radar_data  <- readRDS(file = "data/rds/radar_data.rds")
treemap_data <- readRDS(file = "data/rds/treemap_data.rds")
tree_data <- readRDS(file = "data/rds/tree_data.rds")

heatmap_2012_data <- readRDS(file = "data/rds/heatmap_2012_data.rds")
heatmap_2013_data <- readRDS(file = "data/rds/heatmap_2013_data.rds")
heatmap_2014_data <- readRDS(file = "data/rds/heatmap_2014_data.rds")
heatmap_2015_data <- readRDS(file = "data/rds/heatmap_2015_data.rds")
heatmap_2016_data <- readRDS(file = "data/rds/heatmap_2016_data.rds")
heatmap_2017_data <- readRDS(file = "data/rds/heatmap_2017_data.rds")

choropleth_UF2012_data <- readRDS(file = "data/rds/percentage_UF2012.rds")
choropleth_UF2013_data <- readRDS(file = "data/rds/percentage_UF2013.rds")
choropleth_UF2014_data <- readRDS(file = "data/rds/percentage_UF2014.rds")
choropleth_UF2015_data <- readRDS(file = "data/rds/percentage_UF2015.rds")
choropleth_UF2016_data <- readRDS(file = "data/rds/percentage_UF2016.rds")
choropleth_UF2017_data <- readRDS(file = "data/rds/percentage_UF2017.rds")

choropleth_meso2012_data <- readRDS(file = "data/rds/percentage_meso2012.rds")
choropleth_meso2013_data <- readRDS(file = "data/rds/percentage_meso2013.rds")
choropleth_meso2014_data <- readRDS(file = "data/rds/percentage_meso2014.rds")
choropleth_meso2015_data <- readRDS(file = "data/rds/percentage_meso2015.rds")
choropleth_meso2016_data <- readRDS(file = "data/rds/percentage_meso2016.rds")
choropleth_meso2017_data <- readRDS(file = "data/rds/percentage_meso2017.rds")

server <- function(input, output, session) {

    observe({

        session$sendCustomMessage(type="scatterplot_values", message = list(sct_data, sct_data_b))

    })

    observe({

        session$sendCustomMessage(type="dendrogram_values",
                                  message = list(tree_data,
                                                 bar_data,
                                                 radar_data))

    })

    observe({

        session$sendCustomMessage(type="treemap2_values", message = treemap_data)

    })


    observe({

        session$sendCustomMessage(type="choropleth_values", message = list(
                                                                list(choropleth_UF2012_data,
                                                                     choropleth_UF2013_data,
                                                                     choropleth_UF2014_data,
                                                                     choropleth_UF2015_data,
                                                                     choropleth_UF2016_data,
                                                                     choropleth_UF2017_data),
                                                                list(heatmap_2012_data,
                                                                     heatmap_2013_data,
                                                                     heatmap_2014_data,
                                                                     heatmap_2015_data,
                                                                     heatmap_2016_data,
                                                                     heatmap_2017_data),
                                                                list(choropleth_meso2012_data,
                                                                     choropleth_meso2013_data,
                                                                     choropleth_meso2014_data,
                                                                     choropleth_meso2015_data,
                                                                     choropleth_meso2016_data,
                                                                     choropleth_meso2017_data)
                                                            )
                                  )

    })

    observe({

        session$sendCustomMessage(type="parallelcoordinates_values", message = parallelcoordinates_data)

    })

}
